package com.xsir.ditan.miniprogram.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xsir.ditan.admin.vo.GoodsInfoVo;
import com.xsir.ditan.miniprogram.entity.Banner;
import com.xsir.ditan.miniprogram.entity.Goods;
import com.xsir.ditan.miniprogram.entity.GoodsDate;
import com.xsir.ditan.miniprogram.mapper.BannerMapper;
import com.xsir.ditan.miniprogram.mapper.GoodsDateMapper;
import com.xsir.ditan.miniprogram.mapper.GoodsMapper;
import com.xsir.ditan.miniprogram.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xsir.ditan.miniprogram.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xsir
 * @since 2021-02-06
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {
    @Autowired
    private GoodsDateMapper goodsDateMapper;
    @Autowired
    private BannerMapper bannerMapper;
    
    @Override
    public Map<String, Object> edit(GoodsInfoVo goodsInfoVo) {
        Map<String,Object> data = new HashMap<>(2);
        Goods goods = new Goods();
        BeanUtils.copyProperties(goodsInfoVo, goods);
        String[] period = goodsInfoVo.getPeriod().split("~");
        goods.setStartAt(period[0].trim());
        goods.setEndAt(period[1].trim());
    
        if(goods.getId() == null || goods.getId() == 0) {
            save(goods);
            bannerMapper.insert(new Banner(goods.getId(), goods.getImage()));
            List<String> dates = DateUtil.toList(goods.getStartAt(), goods.getEndAt());
            List<GoodsDate> goodsDateList = new ArrayList<>();
            dates.forEach(date -> {
                GoodsDate goodsDate = new GoodsDate();
                goodsDate.setGoodsId(goods.getId());
                goodsDate.setDate(date);
                goodsDate.setValue(1);
                goodsDateList.add(goodsDate);
            });
            goodsDateMapper.insertBatch(goodsDateList);
            data.put("msg", "添加成功");
        } else {
            updateById(goods);
            data.put("msg", "修改成功");
        }
        data.put("code", 0);
        return data;
    }
    
    @Override
    public Map<String, Object> info(Integer id) {
        if(id == null || id <= 0) {
            return Collections.emptyMap();
        }
        Goods goods = getById(id);
        GoodsInfoVo goodsInfoVo = new GoodsInfoVo();
        BeanUtils.copyProperties(goods, goodsInfoVo);
        goodsInfoVo.setPeriod(goods.getStartAt() + " ~ " + goods.getEndAt());
        
        return JSON.parseObject(JSON.toJSONString(goodsInfoVo), new TypeReference<Map<String,Object>>(){});
    }
    
    @Override
    public Map<String, Object> getList() {
        IPage<Goods> iPage = new Page<>();
        IPage<Goods> goodsPage = page(iPage);
        List<Goods> goods = goodsPage.getRecords();
        List<GoodsInfoVo> goodsInfoVoList = new ArrayList<>();
        
        if(!goods.isEmpty()) {
            goods.forEach(item -> {
                GoodsInfoVo goodsInfoVo = new GoodsInfoVo();
                BeanUtils.copyProperties(item, goodsInfoVo);
                StringBuilder sb = new StringBuilder(item.getStartAt());
                sb.append("~").append(item.getEndAt());
                goodsInfoVo.setPeriod(sb.toString());
                goodsInfoVoList.add(goodsInfoVo);
            });
        }
    
        Map<String,Object> data = new HashMap<>(3);
        data.put("code", 0);
        data.put("data", goodsInfoVoList);
        data.put("count", goodsPage.getTotal());
        
        return data;
    }
    
    @Override
    public Map<String, Object> delete(Integer id) {
        boolean b = removeById(id);
        return new HashMap<String,Object>(2) {
            {
                put("code", 0);
                put("msg", "删除成功");
            }
        };
    }
}
